package com.drinks.yiniang.io.api;

import com.alibaba.fastjson.JSON;
import com.drinks.yiniang.entity.category.CategoryHotBrandJson;
import com.drinks.yiniang.entity.goods.GoodsJson;
import com.drinks.yiniang.entity.shop.MfrsDetailJson;
import com.drinks.yiniang.entity.shop.ShopJson;
import com.drinks.yiniang.io.http.HttpUrls;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.utils.LogUtils;
import com.hansen.library.utils.StringUtils;

/**
 * 店铺api
 * Created by han on 2019/3/6 0006.
 */

public class ShopApiIO extends BaseApiIO {
    volatile static ShopApiIO instance;

    static public ShopApiIO getInstance() {
        if (instance == null) {
            synchronized (ShopApiIO.class) {
                if (instance == null) {
                    instance = new ShopApiIO();
                }
            }
        }
        return instance;
    }

    /**
     * 获取店铺列表
     *
     * @param lngLat      经度维度
     * @param callback
     */
    public void getShopList(String lngLat, final APIRequestCallback<ShopJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        if(!StringUtils.isEmpty(lngLat)) {
            paramsMap.put("lng_lat", lngLat);
        }

        MainApiIO.getInstance().postRequest(HttpUrls.API_SHOP_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("获取店铺列表===onSuccess");

                ShopJson mDataJson = JSON.parseObject(res, ShopJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    //--------------------------------------------厂商Api

    /**
     * 厂商详情
     *
     * @param mfrsId   厂商id
     * @param callback
     */
    public void getMfrsBrandDetail(String mfrsId, final APIRequestCallback<MfrsDetailJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("brand_id", mfrsId);

        MainApiIO.getInstance().postRequest(HttpUrls.API_MFRS_DETAIL, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("厂商详情===onSuccess");

                MfrsDetailJson mDataJson = JSON.parseObject(res, MfrsDetailJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }
}
